//
//  HHPCareViewController.m
//  HHPLiveVideo
//
//  Created by 韩红坡 on 2017/4/17.
//  Copyright © 2017年 韩红坡. All rights reserved.
//

#import "HHPCareViewController.h"
#import "HHPCommonViewModel.h"
#import "TYTabButtonPagerController.h"
#import "HHPHomeViewController.h"

typedef NS_ENUM(NSUInteger, ScrollType) {
    ScrollTypeUp,
    ScrollTypeDown,
};

@interface HHPCareViewController () <UIScrollViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) HHPTableViewManager *tableViewManager;

@property (nonatomic, strong) HHPCommonViewModel *commonViewModel;

@property (nonatomic, assign) CGFloat lastContentOffsetY;

@property (nonatomic, assign) ScrollType scrollType;

@end

@implementation HHPCareViewController
#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initViews];
    
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat currentOffsetY = scrollView.contentOffset.y;
    if (currentOffsetY <= 0 || currentOffsetY + ScreenHeight >= scrollView.contentSize.height) {
        return;
    }
    
    CGFloat delta = currentOffsetY - self.lastContentOffsetY;
    
    HHPLog(@"%s, %d, delta = %f", __func__, __LINE__, delta);
    
    TYTabButtonPagerController *pagerController = (TYTabButtonPagerController *)self.parentViewController;
    UIView *pagerBarView = pagerController.pagerBarView;
    UIView *contentView = pagerController.contentView;
    UIView *tabBar = self.tabBarController.tabBar;
    
    if (self.lastContentOffsetY < currentOffsetY) {
        HHPLog(@"%s, %d, 向上滚动", __func__, __LINE__);
        //向上滚动
        self.scrollType = ScrollTypeUp;
        
        if (pagerBarView.bottom > 0) {
            CGFloat bottom = pagerBarView.bottom - delta;
            
            if (bottom < 0) {
                pagerBarView.bottom = 0;
            } else {
                pagerBarView.bottom = bottom;
            }
        }
        
        if (tabBar.bottom < ScreenHeight + 49) {
            CGFloat bottom = tabBar.bottom + delta;
            
            if (bottom > ScreenHeight + 49) {
                tabBar.bottom = ScreenHeight + 49;
            } else {
                tabBar.bottom = bottom;
            }
        }
        
        if (contentView.height < ScreenHeight) {
            CGFloat height = contentView.height += delta;
            
            if (contentView.height > ScreenHeight) {
                contentView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
            } else {
                contentView.frame = CGRectMake(0, ScreenHeight - height, ScreenWidth, height);
            }
        }
        
    } else {
        HHPLog(@"%s, %d, 向下滚动", __func__, __LINE__);
        //向下滚动
        self.scrollType = ScrollTypeDown;
        
        if (pagerBarView.bottom < 64) {
            CGFloat bottom = pagerBarView.bottom - delta;
            
            if (bottom > 64) {
                pagerBarView.bottom = 64;
            } else {
                pagerBarView.bottom = bottom;
            }
        }
        
        if (tabBar.bottom > ScreenHeight) {
            CGFloat bottom = tabBar.bottom + delta;
            
            if (bottom < ScreenHeight) {
                tabBar.bottom = ScreenHeight;
            } else {
                tabBar.bottom = bottom;
            }
        }
        
        if (contentView.height > ScreenHeight - 64) {
            CGFloat height = contentView.height += delta;
            
            if (height < ScreenHeight - 64) {
                contentView.frame = CGRectMake(0, 64, ScreenWidth, ScreenHeight);
            } else {
                contentView.frame = CGRectMake(0, ScreenHeight - height, ScreenWidth, height);
            }
        }
    }
    
    self.lastContentOffsetY = currentOffsetY;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    TYTabButtonPagerController *pagerController = (TYTabButtonPagerController *)self.parentViewController;
    
    if (self.scrollType == ScrollTypeUp) {
        //向上
        if (pagerController.pagerBarView.bottom <= 0) {
            [self showAnimationWithHidden:YES];
        } else {
            [self showAnimationWithHidden:NO];
        }
    } else {
        //向下
        if (pagerController.pagerBarView.bottom >= 49) {
            [self showAnimationWithHidden:NO];
        } else {
            [self showAnimationWithHidden:YES];
        }
    }
}

- (void)showAnimationWithHidden:(BOOL)hidden
{
    TYTabButtonPagerController *pagerController = (TYTabButtonPagerController *)self.parentViewController;
    
    if (hidden) {
        [UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
            self.tabBarController.tabBar.top = ScreenHeight;
            pagerController.pagerBarView.bottom = 0;
            pagerController.contentView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
        } completion:nil];
    } else {
        [UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
            self.tabBarController.tabBar.top = ScreenHeight - 49;
            pagerController.pagerBarView.bottom = 64;
            pagerController.contentView.frame = CGRectMake(0, 64, ScreenWidth, ScreenHeight - 64);
        } completion:nil];
    }
}

#pragma mark - private methods
- (void)initViews
{
    [self.view addSubview:self.tableView];
    
    [self layoutPageSubviews];
}

- (void)layoutPageSubviews
{
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.view);
    }];
}

- (void)reloadData
{
    [self.tableView hhp_reloadDataWithBlock:^{
        [self.tableView hhp_addCells:self.commonViewModel.hotLiveArray];
    }];
}

#pragma mark - getter and setter
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.dataSource = self.tableViewManager;
        _tableView.delegate = self.tableViewManager;
        _tableView.backgroundColor = ViewBgColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        @weakify(self)
        _tableView.mj_header = [HHPRefreshGifHeader headerWithRefreshingBlock:^{
            @strongify(self)            
            [self.commonViewModel fetchMyOnlineFriendsDataWithCompletionBlock:^(BOOL success) {
                [self.tableView.mj_header endRefreshing];
                if (success) {
                    [self reloadData];
                }
            }];
        }];
        
        _tableView.mj_footer.hidden = YES;
    }
    
    return _tableView;
}

- (HHPTableViewManager *)tableViewManager
{
    if (!_tableViewManager) {
        _tableViewManager = [[HHPTableViewManager alloc] init];
        _tableViewManager.delegate = self;
    }
    
    return _tableViewManager;
}

- (HHPCommonViewModel *)commonViewModel
{
    if (!_commonViewModel) {
        _commonViewModel = [[HHPCommonViewModel alloc] init];
    }
    
    return _commonViewModel;
}

@end
